-- 运营活动: 达标竞技


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动  
    act_id = 5, -- 活动id (5 经验竞技)
    act_name = '经验竞技', -- 活动名称
    act_desc = '达标其中某个活动则可以领取对应奖励', -- 活动描述
    beg_time = 1488956400,  -- 活动开始时间(unix timestamp)
    end_time = 1489042800,  -- 活动结束时间(unix timestamp)  
    config = { -- 配置数据
        Awards =
        {
            {
                desc = Lang.Activity.operationExpDesc01,
                icon = 1,
                needValue = 400,      --获得奖励类型值得数量
                maxCount = 3,
                mailReward = 1,             --邮件奖励，不能领取
                awards = 
                {
                    { type = 0, id = 753, count = 1, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 509, count = 10, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 560, count = 1, quality = 0, strong = 0, bind = 1, },
                },
            },
            {
                desc = Lang.Activity.operationExpDesc02,
                icon = 2,
                needValue = 300,      --获得奖励类型值得数量
                needBroadcast = true,
                maxCount = 10,
                awards = 
                {
                    { type = 0, id = 509, count = 6, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 763, count = 200, quality = 0, strong = 0, bind = 1, },
                },
            },
            {
                desc = Lang.Activity.operationExpDesc03,
                icon = 3,
                needValue = 200,      --获得奖励类型值得数量
                needBroadcast = true,
                maxCount = 30,
                awards = 
                {
                    { type = 0, id = 509, count = 3, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 763, count = 100, quality = 0, strong = 0, bind = 1, },
                },
            },
            {
                desc = Lang.Activity.operationExpDesc04,
                icon = 4,
                needValue = 100,      --获得奖励类型值得数量,
                needBroadcast = true,
                maxCount = 60,--配置为-1不限制数量
                awards = 
                {
                    { type = 0, id = 509, count = 1, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 763, count = 50, quality = 0, strong = 0, bind = 1, },
                },
            },
            {
                desc = Lang.Activity.operationExpDesc05,
                icon = 6,
                needValue = 50,      --获得奖励类型值得数量
                needBroadcast = true,
                maxCount = -1,--配置为-1不限制数量
                awards = 
                {
                    { type = 0, id = 508, count = 1, quality = 0, strong = 0, bind = 1, },
                    { type = 0, id = 514, count = 1, quality = 0, strong = 0, bind = 1, },
                },
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_33_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.Awards == nil then return 23 end
    if type(config.Awards) ~= "table" then return 24 end
    for idx = 1, #config.Awards do
        local dayRewards = config.Awards[idx]
        if dayRewards == nil then return 25 end
        if type(dayRewards) ~= "table" then return 26 end
        if dayRewards.awards == nil then return 27 end
        if type(dayRewards.awards) ~= "table" then return 28 end
        if not oper_act_check_one_award_cfg(dayRewards.awards) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_33_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_33_unsetup(cfg)   
end

-- 活动开始
function oper_act_33_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_33_start = 1
    ssVar.oper_act_33_cmd = cfg.cmd_id
    ssVar.oper_act_33_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_33, 1)

    local needClearState = true

    local crossDayState = false

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            needClearState = false
            local time = System.getSystemTime()
            if myData and myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
                crossDayState = true
            end
        end
    end

    if crossDayState then
        if cfg then
            local config = cfg.config
            if config then 
                local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_33].strName)
                if rankPtr then
                    local rankCount = Ranking.getRankItemCount(rankPtr)
                    for idx = 1, rankCount do
                        local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
                        if item then
                            local myScore = Ranking.getPoint(item)    
                            if myScore < config.needMinValue then
                                break
                            else
                                local awardConfig = GetElemByCond(config.Awards, idx)
                                if not awardConfig then
                                    break
                                end
                                local actorId = tonumber(Ranking.getId(item))
                                local title = string.format(Lang.ScriptTips.Operation101, idx)
                                local content = string.format(Lang.ScriptTips.Operation102, idx)
                                
                                local data = gRankMgr_GetValueById(actorId, RANK_DEFINE_OPERATION_33, 1)
                                local myData = BaseFuc_unserialize(data)

                                Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.OperationLogId033, Lang.LogTips.Operation033, myData.vocation, myData.sex)
                            end
                        end
                    end
                end
            end
        end
    end

    if needClearState or crossDayState then
        gRankMgr_Clear(RANK_DEFINE_OPERATION_33)
    end
end

-- 活动结束
function oper_act_33_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_33_start = nil
    ssVar.oper_act_33_cmd = nil
    ssVar.oper_act_33_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_33].strName)
    if rankPtr then
        local rankCount = Ranking.getRankItemCount(rankPtr)
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
            if item then
                local myScore = Ranking.getPoint(item)    
                if myScore < config.needMinValue then
                    return
                else
                    local awardConfig = GetElemByCond(config.Awards, idx)
                    if not awardConfig then
                        return
                    end
                    local actorId = tonumber(Ranking.getId(item))
                    local title = string.format(Lang.ScriptTips.Operation101, idx)
                    local content = string.format(Lang.ScriptTips.Operation102, idx)

                    local data = gRankMgr_GetValueById(actorId, RANK_DEFINE_OPERATION_33, 1)
                    local myData = BaseFuc_unserialize(data)

                    Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.OperationLogId033, Lang.LogTips.Operation033, myData.vocation, myData.sex)
                end
            end
        end
    end
end

-- 打包数据
function oper_act_33_packet_data(sysarg, cfg)
    if not cfg then return end
    local config = cfg.config
    if not config then return end
    
    local myIdx = 0
    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_33].strName)
    if rankPtr then
        local actorid = Actor.getActorId(sysarg)
        local item = Ranking.getItemPtrFromId(rankPtr, actorid)
        if item then
            myIdx = Ranking.getIndexFromPtr( item ) + 1
            local myScore = Ranking.getPoint(item)    
            if myScore < config.needMinValue then
                myIdx = 0
            end
        end
    end
    
    local npack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (npack == nil) then
        return
    end

    DataPack.writeShort(npack, myIdx)

    DataPack.flush(npack) 
end

-- 操作，返回操作结果，0为成功
function oper_act_33_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_33_do cfg.config confg error")
        return
    end

end


-- 跨天
function oper_act_33_new_day_arrive(act_id) 

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_33_start then
        return
    end
    if not ssVar.oper_act_33_cmd then
        return
    end
    if not ssVar.oper_act_33_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_33_cmd, ssVar.oper_act_33_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_33].strName)
    if rankPtr then
        local rankCount = Ranking.getRankItemCount(rankPtr)
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
            if item then
                local myScore = Ranking.getPoint(item)    
                if myScore < config.needMinValue then
                    return
                else
                    local awardConfig = GetElemByCond(config.Awards, idx)
                    if not awardConfig then
                        return
                    end
                    local actorId = tonumber(Ranking.getId(item))
                    local title = string.format(Lang.ScriptTips.Operation101, idx)
                    local content = string.format(Lang.ScriptTips.Operation102, idx)

                    local data = gRankMgr_GetValueById(actorId, RANK_DEFINE_OPERATION_33, 1)
                    local myData = BaseFuc_unserialize(data)

                    Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.OperationLogId033, Lang.LogTips.Operation033, myData.vocation, myData.sex)
                end
            end
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_33)
end


function oper_act_33_get_value_arrive(sysarg, value)
    if value <= 0 then
        return
    end

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_33_start then
        return
    end
    if not ssVar.oper_act_33_cmd then
        return
    end
    if not ssVar.oper_act_33_act then
        return
    end
    
    local cfg = get_oper_act_cfg(ssVar.oper_act_33_cmd, ssVar.oper_act_33_act)
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end 

    local actorid = Actor.getActorId(sysarg)

    --local value = gRankMgr_GetValue(actorid, RANK_DEFINE_OPERATION_33)

    gRankMgr_AddValue(actorid, RANK_DEFINE_OPERATION_33, value)

    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_33, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end
    
    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

    myData.mySelfTime = time
    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_33, 1, newData)

    
    oper_act_33_packet_data(sysarg, cfg)
    
end
